package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CourseOrderQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseOrderService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CourseOrderQuery qo) {
        PageInfo courseOrderList = courseOrderService.list(qo);
        model.addAttribute("result", courseOrderList);
        //根据目录的sn查客户的所有来源source
        List<SystemDictionaryItem> subject = customerService.selectJobBysn("subject");
        model.addAttribute("subject", subject);
        List<Customer> customers = customerService.selectCustomer();
        model.addAttribute("customers", customers);
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectCourse();
        model.addAttribute("systemDictionaryItems", systemDictionaryItems);
        return "courseOrder/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseOrder) {
        if (courseOrder.getId() == null) { // 代表新增
            courseOrderService.save(courseOrder);
        } else { // 代表修改
            courseOrderService.update(courseOrder);
        }
        return new JsonResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"courseOrder:delete", "部门删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        courseOrderService.delete(id);
        return new JsonResult();
    }
}
